<?php

if(isset($_GET['language']))
{
	$language=$_GET['language'];
}

if(isset($_GET['lang']))
{
	if(in_array($_GET['lang'],$Languages))
	{
	   $language       = $_GET['lang'];
		@gpc_setCookie('lang',$language,0,0,"/");
		if(strpos($_SERVER['HTTP_REFERER'],"?lang")===false)
		{
            header("location: ".$folder_path_root);
			exit();
			if(isset($_GET['ref']))
			{
				header("location: /".$_GET['ref']."/");
				exit();
			}
			if(empty($_SERVER['HTTP_REFERER']))
			{
				header("location: /");
				exit();
			}else
			{
				header("location: ".$_SERVER['HTTP_REFERER']);
				exit();
			}
		}
	}
}

if(isset($_COOKIE['lang']))
{
	if(in_array($_COOKIE['lang'],$Languages))
	{
		$language=$_COOKIE['lang'];		
	}
}


function getLang($str)
{
    global $langarray,$language;
    if(empty($langarray) || !isset($langarray))
    {
        //$timestart  = microtime(true);
        $langarray  = readLang($language);  
        //echo  microtime(true)- $timestart;
    }
    $url=preg_replace("([^a-zA-Z0-9])","",$str);
    $url=strtoupper($url);
    if(isset($langarray[$url]))
    {
        return ($langarray[$url]);
    }    
    $langarray[$url] = $str;
    @writeLang($langarray);    
    return ($str);
}

function writeLang($langarray)
{    
    global $language;
    $file   = 'language/'.$language.'.dat';
    
    if(!is_array($langarray) || count($langarray)==0)
    {
        return false;
    } 
     
    $f      = @fopen($file,'w+');  
    @fwrite($f,@serialize($langarray));
    @fclose($f);
}

function readLang($language)
{
    $file   = 'language/'.$language.'.dat';
    if (!file_exists($file)) 
    {
        @touch($file);
    }
    
    return @unserialize(@file_get_contents($file));
}
?>